효율적인 다중스레드 프로그램 설계는 어떻게 해야하나요?

cho's의 이미지

현재 다중스레드 상에서 작업을 하고 있는데 소스코드가 너무 지저분해졌습니다.
그래서 제가 생각한 최선의 방법을 설명드리고 다른 경험적인 조언을 듣고 싶습니다.
현재 비디오를 갭쳐해서 압축하고 저장을 하고 있습니다. 스레드간에 큐에 데이타를 넣어서 전달하지요.
먼저 스레드는
1. 오디오/비디오 캡쳐스레드
2. 오디오/비디오 저장스레드
3. 오디오/비디오 원격서비스 스레드
다음과 같습니다.
근데 고민은 thread를 제어하는 방법입니다. (이미 queue안에서 pthread_mutex_lock, pthread_cond_wait, pthread_signal)을
사용하고 있기때문에 데이터 동기화는 이루어지고 있습니다.
이 루틴 외부 즉 app쪽에서 사용자가 stop, pause, play, resume등을 하려고 하는데 , 각각의 control하는 플래그를
전역변수로 잡아놓고서 값이 바뀌면 위의 4가지 동작이 이루어지도록 하고 있습니다.
근데 소스도 지저분하고 모든 스레드들의 기본동작이 일치하니까 코드를 일관성을 유지하고 싶습니다.방법이 없을까요?
제가 생각한 방법1: 동일한 인터페이스를 추상화 시켜서 준다.]


#ifndef __XTHREAD_H__
#define __XTHREAD_H__
typedef struct xthread_func_s{
  int (*control)(int cmd, int arg);
  int (*stop)();
  int (*pause)();
  int (*play)();
  int (*resume)();
}xthread_func_t;
extern xthread_func_t audio_thread;
//extern xthread_func_t video_thread;
//extern xthread_func_t audio_record;
//extern xthread_func_t video_record;

#endif

#include "thread.h"
static int control(int, int);
static int stop();
static int pause();
static int play();
static int resume();

xthread_func_t audio_thread = { control, stop, pause, resume };

static int control(int cmd, int arg) { printf("control\n"); }
static int stop() { printf("stop\n"); }
static int pause() { printf("pause\n"); }
static int play() { printf("play\n"); }
static int resume() { printf("resume\n"); 

#include "thread.h"

int main(int argc, char* argv[])
{
  xthread_func_t* audioT = &audio_thread;
  audioT->play();
}

[방법2]
윈도에서 처럼 waitMultipleObject를 통해서 시그널을 기다린다. 그런데 리눅스에서는 multiplewait이 안되는것 같더군요.

결론적으로는 이런방식으로 스레드가 동작하는 프로그램은 어떻게 짜는것이 효과적일까요? 현재까지 제가 생각한 방법은 1번인데요.
의견을 나눠보고 싶습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.