谁能给我讲讲 关于线程 和线程同步的知识,都有那几种方法! 有Event Semaphore Mutex结合Wait函数的使用 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不好意思搞错了Unix下的线程同步不是很清楚 建立线程我到知道是fork UNIX不是任何系统都支持线程的 我到那里能找到这样的例子,贴上来好吗!?!多谢!不用API MFC库 有什么好的办法吗!??? windows下的有mutex,semaphore,event,criticalsection,timer建议你看看richard steves的unix网络编程,我记得讲同步再第二卷上 UNIX下你可以使用互斥锁(mutex):#include<pthread.h>int pthread_mutex_lock(pthread_mutex_t *mptr);int pthread_mutex_unlock(pthread_mutex_t *mptr);定义一个全局变量:pthread_mutex_t nMutex=PTHREAD_MUTEX_INITIALIZER //不同的系统可以有不同的初值在线程函数中:pthread_mutex_lock(&nMutex);//处理数据pthread_mutex_unlock(&nMutex); 给你一个例子:#include <pthread.h>#include <stdlib.h>#include <unistd.h>#include <stdio.h>int myglobal;pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER; void *thread_function(void *arg) { int i,j; for ( i=0; i<20; i++) { pthread_mutex_lock(&mymutex); j=myglobal; j=j+1; printf("."); fflush(stdout); sleep(1); myglobal=j; pthread_mutex_unlock(&mymutex); } return NULL;}int main(void) { pthread_t mythread; int i; if ( pthread_create( &mythread, NULL, thread_function, NULL) ) { printf("error creating thread."); abort(); } for ( i=0; i<20; i++) { pthread_mutex_lock(&mymutex); myglobal=myglobal+1; pthread_mutex_unlock(&mymutex); printf("o"); fflush(stdout); sleep(1); } if ( pthread_join ( mythread, NULL ) ) { printf("error joining thread."); abort(); } printf("\nmyglobal equals %d\n",myglobal); exit(0);} 如何给SD卡发送命令? 我用vs创建串口通信程序的时候报错 双缓冲画图时的问题 CWND派生的类能不能串行化!对不起分不多了 如何作才能在任何文件或者文件夹被右键选中的时候弹出我的菜单?? 关于RasGetLinkStatistics函数问题,请大侠指教(急) VC中有个函数不执行,被跳过 请问什么控件可以 自动 与本地磁盘文件夹同步!!! sdk的安装 关于tabctrl控件的问题 请问OnPaint()中的dc在内存中的位置始终不变吗?也就是说我可不可以使用一个全局变量将其保存起来。 怎样在一个sdi的程序中不要菜单,把File菜单全部拿掉?
不用API MFC库
有什么好的办法吗!???
建议你看看richard steves的unix网络编程,我记得讲同步再第二卷上
#include<pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mptr);
int pthread_mutex_unlock(pthread_mutex_t *mptr);定义一个全局变量:
pthread_mutex_t nMutex=PTHREAD_MUTEX_INITIALIZER //不同的系统可以有不同的初值在线程函数中:
pthread_mutex_lock(&nMutex);
//处理数据
pthread_mutex_unlock(&nMutex);
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>int myglobal;
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER; void *thread_function(void *arg) {
int i,j;
for ( i=0; i<20; i++) {
pthread_mutex_lock(&mymutex);
j=myglobal;
j=j+1;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
pthread_mutex_unlock(&mymutex);
}
return NULL;
}int main(void) { pthread_t mythread;
int i; if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
printf("error creating thread.");
abort();
} for ( i=0; i<20; i++) {
pthread_mutex_lock(&mymutex);
myglobal=myglobal+1;
pthread_mutex_unlock(&mymutex);
printf("o");
fflush(stdout);
sleep(1);
} if ( pthread_join ( mythread, NULL ) ) {
printf("error joining thread.");
abort();
} printf("\nmyglobal equals %d\n",myglobal); exit(0);}