Tag: multithreading

multithreadingudp服务器

我是最新的线程,如果你可以建议我,请。 我有服务器向客户端广播消息。 然后客户端将回复发送回服务器。 我想使用单独的线程处理每个回复。 每个回复都有mesage id和thread id。 如何从所有线程中使用此信息填充某些结构,然后阅读它 同样来自我的代码,是否正确地创建线程,或者它是否存在以创建线程只是我得到客户端的回复? 我是从正确的理解开始的吗? int main(){ while(1){ sendto on broadcast IP pthread_create(&cln_thread, NULL, msg_processing, (void *) &arg)) } } msg_processing () { recvfrom client msg with id of packet and thread id how to write those informations and then read them in main when program finish } 谢谢

在线程内打印参数

void * consumer_child(void *arguments){ Range rng = *((Range *) arguments); //prinnting with the range to easily identify each thread printf(“consumer_child[%d-%d] started\n”, rng.start, rng.end ); pthread_exit(0); } 当我打印它时,它打印内存位置,而不是值。 我需要打印这个值。 在主线程中,正确设置起始值和结束值。 我检查了他们。 在主要我把论点设置如下 Range *rng = malloc(sizeof(*rng)); rng->start = i * numbersPerChild; rng->end = (numbersPerChild * (i + 1)) -1 ; printf(“Range for thread %d is %d […]

在OS X上使用crypt_r

我想在Mac OS X 10.8.2上使用crypt_r函数 #define _GNU_SOURCE #include 产生 crypt.h: No such file or directory 我在哪里可以获得crypt.h文件? 还是我错了? 编辑问题 – 具体例子 #include #include int main(){ struct crypt_data * data = (struct crypt_data *) malloc(sizeof(struct crypt_data)); char * testhash; testhash = crypt_r(“string”, “sa”, data); free(data); return 0; } 产生 gcc test.c -Wall test.c: In function ‘main’: test.c:5: error: […]

pthread参数的值已更改

这是我的主要function,其中NO_RECIEVERS = 3 。 我试图使用我发送的i的值来区分编写器和读取器线程。 int main() { int status, i; pthread_t tr[NO_RECIEVERS], tw[NO_SENDERS], bd; i=1; for(i=1; i<=NO_SENDERS; i++) { pthread_create(&tw[i-1], NULL, writer, &i); } for(i=1; i<=NO_RECIEVERS; i++) { printf("%d\n", i); pthread_create(&tr[i-1], NULL, reader, &i); } pthread_create(&bd, NULL ,daemon_thread, NULL); for(i=1; i<=NO_SENDERS; i++) { pthread_join(tw[i-1], NULL); } for(i=1; i<=NO_RECIEVERS; i++) { pthread_join(tr[i-1], NULL); } pthread_join(bd, […]

音频样本生成multithreadingOSX

这个问题是前一个问题的后续问题( 音频制作者与OSX AudioComponent消费者线程和C中的回调线程 ),包括一个测试示例,它的工作和行为符合预期,但并不能完全回答这个问题。 我已经基本上改写了这个问题,并重新编写了示例,因此它只包含普通的C代码。 (我发现前一个例子中几乎没有Objective-C部分的代码只会引起混淆,并使读者分心于问题中必不可少的内容。) 为了利用多个处理器内核以及使CoreAudio拉模型渲染线程尽可能轻量化,LPCM样本的生产者例程显然必须“坐在”不同的线程上,超出实际优先级渲染线程/回调。 它必须将样本提供给循环缓冲区(在此示例中为TPCircularBuffer ),系统将从inNumberFrames的常量中调度数据拉出。 Grand Central Dispatch API提供了一个简单的解决方案,我在一些个人研究(包括试错编码)中推断出了这个解决方案。 这个解决方案很优雅,因为它不会阻止推送和拉动模型之间的任何冲突。 然而,应该处理“子线程”的GCD 到目前为止还没有满足生产者代码的工作线程的特定并行化要求,所以我必须明确地产生一些POSIX线程,具体取决于数量逻辑核心可用。 尽管在加速计算方面结果已经非常显着,但我仍然觉得混合POSIX和GCD有点不合适。 特别是它适用于变量wait_interval ,并且正确地计算它,而不是通过预测渲染线程在下一个循环中需要多少PCM样本。 这是我的测试程序的缩写和简化(伪)代码,在plain-C中。 控制器声明: #include “TPCircularBuffer.h” #include #include #include #include #include typedef struct { TPCircularBuffer buffer; AudioComponentInstance toneUnit; Float64 sampleRate; AudioStreamBasicDescription streamFormat; Float32* f; //array of updated frequencies Float32* a; //array of updated amps Float32* prevf; //array of […]

什么时候pthread_attr_t不是NULL?

除了pthread_attr_t之外,来自POSIX线程的pthread_create的所有参数都非常简单易懂。 pthread_attr_t是什么,如何以及何时不应该被NULL初始化? 我浏览了Linux 手册页 。 我发现有关pthread_attr_t的描述是: 句法: int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void*),void *arg); 说明: The attr argument points to a pthread_attr_t structure whose contents are used at thread creation time to determine attributes for the new thread; this structure is initialized using pthread_attr_init(3) and related functions. If attr is NULL, then […]

从新线程C调用方法

我正在研究一个项目(不是家庭作业),在C中构建一个multithreading的数独解决方案validation器。我是C的新手,所以请原谅我的代码质量不好,因为我还在改进。 我想从9个单独的线程中调用方法row_check 9次。 对于作为参数的方法,我传递行号( arg )和数组名称( arr )。 我创建了线程,但我不确定如何将参数正确传递给方法。 谁能帮我这个? #include #include #include #include void* row_check(void* arg, int *arr) { int i = *((int *)arg); //trying to convert row number to int int j, flag; while(i < 9) { flag=0x0000; for(j = 0; j < 9; j++) flag |= 1 << (arr[i][j]-1); if (flag != […]

圆形链表在c窗口中无法正常工作

我试图通过服务器端应用程序实现循环链​​表来管理一组任务。 应用程序是multithreading的,其中一个线程( updater() )仅读取链接列表以进行读取,而另外两个( push_stream() and delete_stream() )访问链接列表以分别添加和删除链接列表。 我的问题不是删除所有要删除的文件(处理后)。 struct data_stream { bool processed; int count; char filename[30]; int TYPE_GRP; int task_type; struct data_stream *next; }; struct data_stream *stream_head=NULL; //global variable 主要(部分代码) main() { _beginthread(updater, 0, NULL); while ((new_socket = accept(srv_sock, (struct sockaddr *)&client, &c)) != INVALID_SOCKET) { _beginthreadex(0, 0, handle_client, &new_socket, 0, 0); } […]

成功swapcontext后返回函数执行时出现段错误

我正在尝试编写一个库来管理使用上下文的线程(getcontext,setcontext,makecontext,swapcontext),而不使用pthreads。 函数MyThreadYield(),暂停当前线程上下文,将其发送到就绪队列的末尾,并开始在就绪队列的头部执行线程。 在MyThreadYield()中,我能够通过swapcontext()检索暂停线程的上下文,但是当它返回到函数执行时,我得到一个段错误。 例如: – 假设线程1是init线程,它运行并创建线程2.然后它产生。 现在线程2运行,然后调用yield。 这里swapcontext运行,我可以通过“%p”validation交换是否成功。 但是当它试图从MyThreadYield()返回以恢复线程1的函数运行时,我遇到了seg错误。 这是我的图书馆代码: – typedef void *MyThread; typedef void *MySemaphore; #include #include #include #include #include // structure of threads struct tnode { ucontext_t* ctextptr; int tid; // own thread id int pid; // parent thread id } ; struct tnode* cthread = NULL; // linked list for ready […]

如何测量C中每个线程的时间?

我正在研究具有多个线程的代码,我想打印完成任务所花费的时间,我分配了第i个线程。 含义我想打印每个线程使用doSomeThing函数完成的时间 int main(int argc, char *argv[]){ // … i=0; while (i < NumberOfThreads){ check = pthread_create(&(id_arr[i]), NULL, &doSomeThing, &data); i++; } // … } void* doSomeThing(void *arg){ // … } 如果我在pthread_create之前添加gettimeofday(&thread_start, NULL)然后在pthread_create之后添加gettimeofday(&thread_end, NULL) ,我实际上会测量每个线程花费的时间还是主要花费的时间? 如果我把gettimeofday放在doSomething函数中,它们不会创建竞争条件吗? 如果您对如何测量每个线程的时间有任何想法,请告诉我,谢谢。