problem 1
子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码
pthread_cond_t pcond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
bool isMainThreadTurn = false;
#define operation() printf("1")
const int TOTAL_LOOP = 50;
const int SUB_OP_PER_LOOP = 10;
const int MAIN_OP_PER_LOOP = 100;
void* fun(void* arg) {
for (int loop = 0; loop < TOTAL_LOOP; ++loop) {
pthread_mutex_lock(&mutex);
while (isMainThreadTurn == true) {
pthread_cond_wait(&pcond, &mutex);
}
for (int op = 0; op < SUB_OP_PER_LOOP; ++op) {
operation();
}
printf("\n");
isMainThreadTurn = true;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&pcond);
}
return (void*)0;
}
int main() {
pthread_t tid[1];
pthread_create(tid, NULL, fun, NULL);
// can shorten this part, as basically two parts are almost the same
for (int loop = 0; loop < TOTAL_LOOP; ++loop) {
pthread_mutex_lock(&mutex);
while (isMainThreadTurn == false) {
pthread_cond_wait(&pcond, &mutex);
}
for (int op = 0; op < MAIN_OP_PER_LOOP; ++op) {
operation();
}
printf("\n");
isMainThreadTurn = false;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&pcond);
}
return 0;
}
problem 2
编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
#define gettid() syscall(__NR_gettid)
pthread_cond_t pcond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t pmutex = PTHREAD_MUTEX_INITIALIZER;
const int TOTAL_LOOP = 10;
const unsigned int THREAD_NUM = 3;
unsigned int ThreadTurnToPrint = 0;
__thread unsigned int tid;
#define operation() printf("|%u|", tid)
void* fun(void* arg) {
unsigned int myTurn = (unsigned int) arg;
tid = gettid();
for (int loop = 0; loop < TOTAL_LOOP; ++loop) {
pthread_mutex_lock(&pmutex);
while (myTurn != ThreadTurnToPrint) {
pthread_cond_wait(&pcond, &pmutex);
}
operation();
ThreadTurnToPrint = ( 1 + ThreadTurnToPrint) % 3;
pthread_mutex_unlock(&pmutex);
pthread_cond_broadcast(&pcond);
}
return (void*)0;
}
int main()
{
pthread_t tid[THREAD_NUM];
for (int i = 0; i < THREAD_NUM; ++i) {
pthread_create(&tid[i], NULL, fun, (void*)i);
}
/*in my way, I waste mainThread, as mainThread can also do part of job*/
for (int i = 0; i < THREAD_NUM; ++i) {
pthread_join(tid[i], NULL);
}
return 0;
}
problem 3
编写一个程序,程序会启动4个线程,向4个文件1.txt,2.txt,3.txt,4.txt里写入数据,每个线程只能写一个值。
线程A:只写1
线程B:只写2
线程C:只写3
线程D:只写4
4个文件1.txt,2.txt,3.txt,4.txt
程序运行起来,4个文件的写入结果如下:
A:12341234...
B:23412341...
C:34123412...
D:41234123...
pthread_cond_t pcond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t pmutex = PTHREAD_MUTEX_INITIALIZER;
const int TOTAL_LOOP = 10;
const int THREAD_NUM = 4;
int fds[THREAD_NUM];
int turns[THREAD_NUM] = {1, 2, 3, 4};
#define operation(num) printf("%d ", num)
__thread int count[THREAD_NUM] = {TOTAL_LOOP, TOTAL_LOOP, TOTAL_LOOP, TOTAL_LOOP};
__thread char buf[1];
__thread int numToPrint;
void* fun(void* arg) {
numToPrint = (int) arg;
buf[0] = numToPrint + '0';
for (int loop = 0; loop < TOTAL_LOOP * THREAD_NUM;) {
pthread_mutex_lock(&pmutex);
while (turns[0] != numToPrint && turns[1] != numToPrint &&
turns[2] != numToPrint && turns[3] != numToPrint)
{
pthread_cond_wait(&pcond, &pmutex);
}
for (int idx = 0; idx < THREAD_NUM; ++idx) {
if (turns[idx] == numToPrint && count[idx]) {
while ( write(fds[idx], buf, sizeof(buf)) < 0 ) {
if (errno != EAGAIN) {
perror("write err.");
exit(0);
}
}
if ( ++turns[idx] > THREAD_NUM) {
turns[idx] = 1;
}
--count[idx];
++loop;
}
}
pthread_mutex_unlock(&pmutex);
pthread_cond_broadcast(&pcond);
}
return (void*)0;
}
int main()
{
char filename[]= "?.txt";
for (int i = 0; i < THREAD_NUM; ++i) {
filename[0] = i + '1';
fds[i] = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
if (fds[i] < 0) {
perror("open fail");
exit(0);
}
}
pthread_t tid[THREAD_NUM];
for (int i = 0; i < THREAD_NUM; ++i) {
pthread_create(&tid[i], NULL, fun, (void*)(i + 1));
}
for (int i = 0; i < THREAD_NUM; ++i) {
pthread_join(tid[i], NULL);
}
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。