对当前正在运行的线程重用pthread_t变量(reusing pthread_t variable for currently running threads)
如果下面的代码会导致未定义的行为,我不确定。
//global pthread_t thread1; void *worker(void *arg){ //do stuff } void spawnThread(){ //init stuff int iret1 = pthread_create( &thread1, NULL, worker, (void*) p); }
我的spawnThread将使用全局线程1创建一个新线程。
如果我当前正在运行一个未完成的线程,当使用thread1变量启动一个新线程时,会不会导致未定义的行为?
如果这是一个问题,将pthread_t变量放在函数本地是否合理? 我认为这可能是问题,因为它会使用堆栈,并且一旦我从我的函数返回,将被删除。
如果我将pthread_t作为本地函数,则不能在我的程序的另一部分中使用pthread_join。 规范的解决方案是让一个互斥锁计数器跟踪当前有多少线程正在运行?
谢谢
I'm abit uncertain if the following code will lead to undefined behavior.
//global pthread_t thread1; void *worker(void *arg){ //do stuff } void spawnThread(){ //init stuff int iret1 = pthread_create( &thread1, NULL, worker, (void*) p); }
My spawnThread will make a new thread using the global thread1.
If I'm currently running a thread that is not finished, will I somehow cause undefined behaviour when starting a new thread using the thread1 variable?
If this is a problem, would it make sense to make my pthread_t variable local to a function? I think it might be problem because it will use the stack, and as soon as i return from my function that will be removed.
If I make my pthread_t local to a function, I can't use the pthread_join in a another part of my program. Is the canonical solution, to have a mutex'ed counter keeping track of how many current threads are running?
thanks
原文:https://stackoverflow.com/questions/6467205
最满意答案
如果您的视图正在生成
el
本身,则可以通过视图的$el
使其轻松触发事件。describe("my test", function(){ var MyModel = Backbone.Model.extend({}); var MyView = Backbone.View.extend({ events: { "click a": "aClicked" }, aClicked: function(e){ e.preventDefault(); this.model.set({foo: "bar"}); }, render: function(){ this.$el.html("<a href='#foo' id='fooLink'>foo</a>"); } }); var myModel; beforeEach(function(){ myModel = new MyModel(); var myView = new MyView({ model: myModel }); myView.render(); // this will "click" the link myView.$("#fooLink").trigger("click"); }) it("should do that stuff", function(){ expect(myModel.get("foo")).toBe("bar"); }); });
If your view is generating the
el
itself, you can cause it to trigger an event easily, through the view's$el
.describe("my test", function(){ var MyModel = Backbone.Model.extend({}); var MyView = Backbone.View.extend({ events: { "click a": "aClicked" }, aClicked: function(e){ e.preventDefault(); this.model.set({foo: "bar"}); }, render: function(){ this.$el.html("<a href='#foo' id='fooLink'>foo</a>"); } }); var myModel; beforeEach(function(){ myModel = new MyModel(); var myView = new MyView({ model: myModel }); myView.render(); // this will "click" the link myView.$("#fooLink").trigger("click"); }) it("should do that stuff", function(){ expect(myModel.get("foo")).toBe("bar"); }); });
相关问答
更多-
如果您的视图正在生成el本身,则可以通过视图的$el使其轻松触发事件。 describe("my test", function(){ var MyModel = Backbone.Model.extend({}); var MyView = Backbone.View.extend({ events: { "click a": "aClicked" }, aClicked: function(e){ e.preventDefault(); ...
-
使用jasmine测试骨干视图时无法创建视图实例(Cannot create view instance while testing the backbone view using jasmine)[2023-01-02]
看起来你没有实例化视图。 var list_view = new employee_list_view({ collection: collection }); Looks like you did not instantiate the view. Do var list_view = new employee_list_view({ collection: collection }); -
我还没有找到一个避免重复app.js的解决方案。 但是这个文件很少改变,所以这不是一个真正的问题。 但是,我对上面的异步处理做了一些改进,使其更加健壮: waitsFor(function() { return JST[path] && JST[path].__compiled__; }, "loading template", 1000); return JST[path]; done(JST[path]);的行done(JST[path]); 然后可以删除。 现 ...
-
这里的问题是全局范围中的依赖项的加载顺序,这是在问题中看不到的。 确保在主干之前加载下划线。 The problem here was the load orders of the dependencies in the global scope that is not seen in the question. Make sure underscore is loaded before backbone.
-
正如评论中所提到的,我发现,这是由于模型定义中的“新”。 所以,改变后, var Photo = new Backbone.Model.extend({ 至 var Photo = Backbone.Model.extend({ 错误消失了。 As mentioned in the comments, I found out that, this is due to the "new" in the model definition. So, after changing, va ...
-
Descibre , it ,...不是在jasmine.js中定义的longuer ,而是在jasmine库中包含的boot.js中定义的。 Descibre, it, ... are no longuer defined in jasmine.js but instead in the boot.js included with jasmine library.
-
buster.js / sinon有什么像`jasmine.any()`吗?(Has buster.js / sinon something like `jasmine.any()`?)[2024-02-22]
你应该看看sinon.match api( http://sinonjs.org/docs/#sinon-match-api ) 使用sinon.match.func上面的例子会变成: var serviceFunction = this.spy(); // or `sinon.spy()` var functionUnderTest = create(serviceFunction); var thing = 'arbitrary/thing' functionUnderTest(thing); ass ... -
使用Sinon测试Backbone.js模型保存而不调用成功回调(Testing Backbone.js Model save using Sinon not calling success callback)[2023-10-24]
Backbone希望响应文本是有效的JSON,并且因为server.respondWith()方法中的响应“OK”而被轰炸。 将方法更改为: server.respondWith([200, {"Content-Type":"text/html","Content-Length":2}, '{"OK":"True"}']); 成功回调正在成功处理。 Backbone expects the response text to be valid JSON and was bombing out because ... -
从精细手册 : 活动目录 这是Backbone.js可以触发的所有内置事件的列表。 您也可以根据需要自由地在模型和视图上触发自己的事件。 [...] “更改” (模型,选项) - 当模型的属性发生变化时。 因此, "change"事件处理程序的第一个参数将是最初触发事件的模型。 例如,给定一个这样的简单设置: var M = Backbone.Model.extend({}); var C = Backbone.Collection.extend({ model: M }); var c = new ...
-
尝试从Jasmine访问Backbone模型时的ReferenceError?(ReferenceError when trying to access Backbone models from Jasmine?)[2022-01-17]
正如我们在Backbone参考中所看到的那样。 构造函数/初始化 new Model([attributes], [options])创建模型实例时,可以传入将在模型上设置的属性的初始值。 需要使用对象文字(键值对)初始化构造函数,如下所示: new Book({ title: "One Thousand and One Nights", author: "Scheherazade" }); 所以你需要将代码更改为: var todo = new Todo({ ...