我需要制作一个对学生测试进行评分的程序。我正在尝试找到一种使用循环而不要写出很多变量的方法。我以为我可以使用#include <stdio.h>
#include <pthread.h>
struct sync_data
{
pthread_mutex_t mutex;
volatile int predicate;
pthread_cond_t cond;
};
void* function_1(void* p)
{
// the function's main code goes here
//signal to other thread that it may now proceed
struct sync_data *psd = p;
pthread_mutex_lock( &psd->mutex );
psd->predicate = 1;
pthread_mutex_unlock( &psd->mutex );
pthread_cond_signal( &psd->cond );
}
void* function_2(void* p)
{
//wait for signal from other thread
struct sync_data *psd = p;
pthread_mutex_lock( &psd->mutex );
while ( !psd->predicate )
pthread_cond_wait( &psd->cond, &psd->mutex );
pthread_mutex_unlock( &psd->mutex );
// the function's main code goes here
}
int main(void)
{
struct sync_data sd;
pthread_t id1;
pthread_t id2;
//init sync_data struct members
pthread_mutex_init( &sd.mutex, NULL );
sd.predicate = 0;
pthread_cond_init( &sd.cond, NULL );
//create and start both threads
pthread_create( &id1, NULL, function_1, &sd );
pthread_create( &id2, NULL, function_2, &sd );
//wait for all threads to finish
pthread_join(id1, NULL);
pthread_join(id2, NULL);
//cleanup
pthread_mutex_destroy( &sd.mutex );
pthread_cond_destroy( &sd.cond );
}
语句,然后在其中放入switch
语句。
例如,在循环中,如果学生对问题一的答案等于if
,则将1加到计数器上,但是由于某种原因,我的代码无法正常工作。
c
答案 0 :(得分:4)
您的代码有很多问题,但是回答问题本身,i
是一个数字,而不是一个字符。请将您的开关盒改为以下:
case 1: // not '1'
这将突出显示下一个问题,您在默认情况前缺少break;
。