我需要2个线程: TC 和 TS ,这样它们由两个主要部分组成,每个部分访问共享数据,并且必须同步线程。同步应该是这样的:
红色代码正在使用共享数据U
,但get U
可以放在TS
主题上的虚线矩形之前。
Xcurrent
可以位于TC
或TS
个任务中,但是send Ucurrent
的共享硬件和get
必须在send
之后结束了。
出现死锁,我无法找到优雅的解决方案。 线程TS:
#include <stdio.h> /* printf, scanf, NULL */ #include <stdlib.h> /* malloc, free, rand */ #define _USE_MATH_DEFINES #include <math.h> #include <windows.h> #include <pthread.h> #include <time.h> // CRLT-C var static int stop = 0; // TIMEING vars __int64 frequencyT, startT, endT = 0; double baseAngleLast; double pendulAngleLast; // THREADING vars bool startedS = false, Usent = false; double * Ucmd; pthread_mutex_t mutexU = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutexBoard = PTHREAD_MUTEX_INITIALIZER; unsigned int Ulenght = 6; pthread_cond_t signal_to_read_state = PTHREAD_COND_INITIALIZER; pthread_cond_t signal_to_read_cmd = PTHREAD_COND_INITIALIZER; DWORD sleepTime = 30; // in milliseconds int currentState = 3; void *sendingCMD(void * ) { double * U_tmp; U_tmp = (double*)malloc(sizeof(double)*Ulenght); startedS = true; printf("Sin\n"); while (stop == 0) { printf("Smu-\n"); //get U pthread_mutex_lock( &mutexU ); printf("Smu..\n"); pthread_cond_wait(&signal_to_read_cmd, &mutexU); memcpy(U_tmp,Ucmd,sizeof(double)*Ulenght); pthread_mutex_unlock( &mutexU ); printf("Smu+\n"); pthread_mutex_lock( &mutexBoard ); for (unsigned int i = 0; i<Ulenght; i++) { //send CMD signal printf("%f ", U_tmp[i]); if (i == Ulenght -1) printf("\n"); } Sleep(sleepTime); // miliseconds currentState = currentState + 1; pthread_cond_signal(&signal_to_read_state); pthread_mutex_unlock( &mutexBoard ); printf("Smb\n"); } printf("Task S terminated. \n"); return (NULL); } void *computingU(void *) { double * U_tmp; U_tmp = (double*)malloc(sizeof(double)*Ulenght); int currentStateTMP =0; bool fisrtLoop = true; printf("Uin\n"); while (stop == 0) { printf("Umb- \n"); // get current state pthread_mutex_lock( &mutexBoard ); if (!fisrtLoop) { printf("UmbFalse \n"); pthread_cond_wait(&signal_to_read_state, &mutexBoard); } else { printf("UmbTrue \n"); fisrtLoop=false; } currentStateTMP =currentState; pthread_mutex_unlock( &mutexBoard ); printf("Umb+ \n"); pthread_mutex_lock( &mutexU ); for (unsigned int i=0;i<Ulenght;i++) { Ucmd[i] = Ucmd[i]+ (double)currentStateTMP/i; } pthread_cond_signal(&signal_to_read_cmd); pthread_mutex_unlock( &mutexU ); printf("Umu\n"); } return (NULL); } void signal_handler(int signal) { stop = 1; } int main(int argc, char* argv[]) { //initializing output buffer to 0[V] Ucmd= (double*)malloc(sizeof(double)*Ulenght); for (unsigned int i=0;i<Ulenght;i++) Ucmd[i] = 0; //init threads int rc1, rc2; pthread_t threadU, threadS; /* Create independent threads each of which will execute functionC */ if( (rc1=pthread_create( &threadU, NULL, &computingU, NULL)) ) { printf("ThreadU creation failed: %d\n", rc1); } if( (rc2=pthread_create( &threadS, NULL, &sendingCMD, NULL)) ) { printf("ThreadS creation failed: %d\n", rc2); } while (stop == 0); printf("Main terminated, closing board in 10ms. \n"); Sleep(10); return 0; }
阻止出现在:
pthread_cond_wait(&signal_to_read_state, &mutexBoard);
处的 TC
TS pthread_cond_wait(&signal_to_read_cmd, &mutexU);
顺便说一下,如果我从VS2010复制粘贴,为什么剂量无法识别stackoverflow我粘贴的代码段?